package com.fanrui.code.message;

import com.fanrui.code.util.CommandsUtil;
import com.fanrui.proto.cmd.CmdWrapper;
import com.google.protobuf.MessageLite;

public class RequestContext {

	private int requestNumber;
	
	private PlayerSession sender;
	
	private boolean responseIsSend;
	
	public RequestContext(int requestNumber, PlayerSession sender) {
		super();
		this.responseIsSend = false;
		this.requestNumber = requestNumber;
		this.sender = sender;
	}

	public Long getPlayerId() {
		return this.sender.getPlayerId();
	}

	/**
	 * 发送（客户端请求对应的）响应数据，注意对于一个Request，只能发送一次Response，再次发送会产生异常。
	 * @param build
	 */
	public void sendResponse(MessageLite response) {
		if (this.responseIsSend) {
			throw new IllegalStateException(
					String.format("Response已经发送一次，不能再次发送。%s %s", this.toString(), response.toString()));
		}
		CmdWrapper wrapper = CommandsUtil.wrapper(requestNumber, response);
		this.sender.send(wrapper);
		this.responseIsSend = true;
	}

}
